unsigned char code bit_pattern[8] = {
    0x80, // 10000000B (第7位为1)
    0x40, // 01000000B (第6位为1)
    0x20, // 00100000B (第5位为1)
    0x10, // 00010000B (第4位为1)
    0x08, // 00001000B (第3位为1)
    0x04, // 00000100B (第2位为1)
    0x02, // 00000010B (第1位为1)
    0x01  // 00000001B (第0位为1)
};
unsigned char code LedChar[] = {  //数码管显示字符转换表 
    0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 
    0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E 
}; 
char is_key_pressed(unsigned char* port, unsigned char key) {
    key = bit_pattern[key];
    return (*port & key) != 0;
}

void delay_ms(unsigned int ms)
{
	unsigned int i = 0;
	unsigned char j=0;
	for(i=0;i<ms;i++)
		for(j=0;j<125;j++);
}
void choose_position(unsigned char* port,unsigned char led) {
    // check led range
    if (led > 7 || led < 0) return;
    // light the led
    led = led <<2;
    *port |= led;
}
void choose_position_on_P2(unsigned char led) {
    // check led range
    if (led > 7 || led < 0) return;
    // light the led
    led = led <<2;
    P2 |= led;
}
void choose_segment(unsigned char* port, unsigned char segment) {
    // check segment range
    if (segment > 15 || segment < 0) return;
    // light the segment
    *port = LedChar[segment];
}
void choose_segment_on_P0(unsigned char segment) {
    // check segment range
    if (segment > 15 || segment < 0) return;
    // light the segment
    P0 = LedChar[segment];
}
void dynamic_display(unsigned char* pos_port, unsigned char* seg_port, unsigned char number) {
    unsigned char i = 0;
    unsigned char addr = 0;
    for(i=0;i<8;i++) {
        if (i=0) addr = number % 10;
        else addr = (number / (10*i))%10;
        *seg_port = 0xff;
        choose_position(pos_port, i);
        choose_segment(seg_port, addr);
    }
}
void dynamic_display_on_P2P0(unsigned char led, unsigned char segment) {
    
    P0 = 0xff;
    choose_led_on_P2(led);
    choose_segment_on_P0(segment);
}


